package top.mccat.common.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.mccat.common.core.domain.bk.BkArrears;
import top.mccat.common.core.domain.bk.BkBase;
import top.mccat.common.core.domain.bk.BkProduct;
import top.mccat.common.core.domain.bk.BkSupplieRecord;
import top.mccat.common.exception.bk.BaseItemNotFoundException;
import top.mccat.common.mapper.bk.*;
import top.mccat.common.service.IBkBaseService;
import top.mccat.common.service.IBkSupplieRecordService;

/**
 * 供货记录Service业务层处理
 * 
 * @author raven
 * @date 2022-11-06
 */
@Service
public class BkSupplieRecordServiceImpl implements IBkSupplieRecordService
{
    @Autowired
    private BkSupplieRecordMapper bkSupplieRecordMapper;

    @Autowired
    private BkProductRecordMapper bkProductRecordMapper;

    @Autowired
    private BkBaseMapper bkBaseMapper;

    @Autowired
    private BkProductMapper bkProductMapper;

    @Autowired
    private IBkBaseService bkBaseService;

    //建议零售价倍率
    @Value("${retailPriceMagnification}")
    private Double retailPriceMagnification;

    /**
     * 查询供货记录
     *
     * @param id 供货记录主键
     * @return 供货记录
     */
    @Override
    public BkSupplieRecord selectBkSupplieRecordById(Integer id)
    {
        return bkSupplieRecordMapper.selectBkSupplieRecordById(id);
    }

    /**
     * 查询供货记录列表
     * 
     * @param bkSupplieRecord 供货记录
     * @return 供货记录
     */
    @Override
    public List<BkSupplieRecord> selectBkSupplieRecordList(BkSupplieRecord bkSupplieRecord)
    {
        return bkSupplieRecordMapper.selectBkSupplieRecordList(bkSupplieRecord);
    }

    /**
     * 新增供货记录
     * 
     * @param bkSupplieRecord 供货记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBkSupplieRecord(BkSupplieRecord bkSupplieRecord) throws BaseItemNotFoundException {
        int row = bkSupplieRecordMapper.insertBkSupplieRecord(bkSupplieRecord);
        bkProductRecordMapper.insertBkProductRecord(bkSupplieRecord);
        BkBase bkBase = bkBaseMapper.selectBkBaseByProductId(bkSupplieRecord.getProductId().intValue());
        if(bkBase != null){
            //进行增加，这里的接口必定是进件操作
            bkBase.setAmount(bkBase.getAmount() + bkSupplieRecord.getPurchaseQuantity());
            bkBaseMapper.updateBkBase(bkBase);
        }else {
            //不存在则自动添加并进行提示
            bkBase = new BkBase();
            bkBase.setAmount(bkSupplieRecord.getPurchaseQuantity());
            bkBase.setProductId(bkSupplieRecord.getProductId());
            //默认设置3为新进状态
            bkBase.setItemLocation(3);
            bkBaseService.insertBkBase(bkBase);
            throw new BaseItemNotFoundException("警告！仓库中暂无该物品，已自动新增一个商品到库存表中。请点击取消键并打开仓库管理来手动设置其存储地点！");
        }
        // 进行进货商品数据更新
        BkProduct product = new BkProduct();
        product.setId(bkSupplieRecord.getProductId().intValue());
        //获取本次采购的价格
        BigDecimal purchasePrice = bkSupplieRecord.getPurchasePrice();
        //获取本次采购数量
        Double purchaseQuantity = bkSupplieRecord.getPurchaseQuantity();
        BigDecimal divide = purchasePrice.divide(BigDecimal.valueOf(purchaseQuantity));
        //设置最新进价
        product.setLatestPurchasePrice(divide);
        //进行建议销售价传递
        product.setRetailPrice(divide.multiply(BigDecimal.valueOf(retailPriceMagnification)));
        bkProductMapper.updateBkProduct(product);
        // 进行欠账新增操作
        return row;
    }

    /**
     * 修改供货记录
     * 
     * @param bkSupplieRecord 供货记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBkSupplieRecord(BkSupplieRecord bkSupplieRecord)
    {
        Integer id = bkSupplieRecord.getId();
        //删除连接表
        bkProductRecordMapper.deleteBkProductRecordByRecordId(id);
        //增加连接表
        bkProductRecordMapper.insertBkProductRecord(bkSupplieRecord);
        return bkSupplieRecordMapper.updateBkSupplieRecord(bkSupplieRecord);
    }

    /**
     * 批量删除供货记录
     * 
     * @param ids 需要删除的供货记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBkSupplieRecordByIds(Integer[] ids)
    {
        int row = bkSupplieRecordMapper.deleteBkSupplieRecordByIds(ids);
        bkProductRecordMapper.deleteBkProductRecordByRecordIds(ids);
        return row;
    }

    /**
     * 删除供货记录信息
     * 
     * @param id 供货记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBkSupplieRecordById(Integer id)
    {
        int row = bkSupplieRecordMapper.deleteBkSupplieRecordById(id);
        bkSupplieRecordMapper.deleteBkSupplieRecordById(id);
        return row;
    }
}
